package Tools;

import java.awt.EventQueue;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;

public class RegexGUI extends JFrame {

	private JPanel contentPane;

	JTextArea answerArea;
	JTextField regexField;
	JTextField nextMeaningField;
	JTextField nextWordField;
	JTextField restField;

	JTextField inputField;

	public RegexGUI() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 300);
		contentPane = new JPanel();
		contentPane.setLayout(new GridLayout(2, 1, 5, 5));
		setContentPane(contentPane);

		// panel for the buttons
		JPanel mainInputPane = new JPanel();
		mainInputPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		mainInputPane.setLayout(new GridLayout(3, 1, 5, 5));

		// panel for the three querey options
		JPanel optionPane = new JPanel();
		optionPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		optionPane.setLayout(new GridLayout(3, 1, 5, 5));

		// panel for the queries and buttons
		JPanel actionPane = new JPanel();
		actionPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		actionPane.setLayout(new GridLayout(1, 2, 5, 5));
		actionPane.add(mainInputPane);
		actionPane.add(optionPane);

		contentPane.add(actionPane);

		// buttons
		JButton jButton = new JButton("parse string with regex");
		jButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				query();
			}
		});
		mainInputPane.add(jButton);

		Font fieldFont = new Font("Verdana", Font.CENTER_BASELINE, 12);
				
		// field for the user word
		regexField = new JTextField();
		regexField.setText("regex");
		regexField.setFont(fieldFont);
		mainInputPane.add(regexField);

		// field for the user word
		inputField = new JTextField();
		inputField.setText("input");
		inputField.setFont(fieldFont);
		mainInputPane.add(inputField);

		// field for the user word
		nextMeaningField = new JTextField();
		nextMeaningField.setText("meaning number");
		optionPane.add(nextMeaningField);

		// field for the user word
		nextWordField = new JTextField();
		nextWordField.setText("word number");
		optionPane.add(nextWordField);

		// field for the user word
		restField = new JTextField();
		restField.setText("rest number");
		optionPane.add(restField);

		// field for the translated words
		answerArea = new JTextArea();
		answerArea.setText("answer");
		answerArea.setEditable(false);
		contentPane.add(new JScrollPane(answerArea));

	}

	private void query() {

		Pattern regexPattern;
		String regex=regexField.getText();
		regex=regex.replace("\\\\", "\\");
		System.out.println(regex);
		try {
			regexPattern = Pattern.compile(regex);
		} catch (PatternSyntaxException e) {
			answerArea.setText("Problem with pattern:\n"+e.getMessage());
			return;
		}

		Matcher m = regexPattern.matcher(inputField.getText());
		if (m.find()) {
			String answer = "";
			try {

				answer += "meaning:\t";
				answer += m.group(Integer.parseInt(nextMeaningField.getText()
						.trim()));
				answer += "\n word:\t";
				answer += m.group(Integer.parseInt(nextWordField.getText()
						.trim()));
				answer += "\n rest:\t";
				answer += m.group(Integer.parseInt(restField.getText().trim()));
			} catch (NumberFormatException e) {
				answerArea.setText("there is a entry that is not a number!");
				return;
			}

			answerArea.setText(answer);
		} else
			answerArea.setText("doesn't match");

	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					RegexGUI frame = new RegexGUI();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});

	}

}
